<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="售后单号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入售后单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单流水号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单项编号" prop="orderItemId">
        <el-input
          v-model="queryParams.orderItemId"
          placeholder="请输入订单项编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="saleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
<!--      <el-table-column label="用户ID" align="center" prop="userId"/>-->
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="售后单号" align="center" prop="no"/>

      <el-table-column label="售后状态" align="center" prop="status">
        <template slot-scope="scope">
          <p>{{status_dict[scope.row.status]['label']}}</p>
        </template>
      </el-table-column>

      <el-table-column label="申请原因" align="center" prop="applyReason"/>
      <el-table-column label="补充描述" align="center" prop="applyDescription"/>

      <el-table-column label="补充凭证图片" align="center" prop="applyPicUrls">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img :src="scope.row.applyPicUrls" style="width:300px"/>
            <img :src="scope.row.applyPicUrls" style="max-width: 50px;max-height: 50px" slot="reference">
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderId"/>
      <el-table-column label="订单流水号" align="center" prop="orderNo"/>
      <el-table-column label="订单项编号" align="center" prop="orderItemId"/>
      <el-table-column label="商品 SPU 编号" align="center" prop="spuId"/>
      <el-table-column label="商品 SPU 名称" align="center" prop="spuName"/>

      <el-table-column label="商品类型" align="center" prop="spuType">
        <template slot-scope="scope">
          <p>{{spuType_dict[scope.row.spuType - 1]['label']}}</p>
        </template>
      </el-table-column>

      <el-table-column label="商品图片" align="center" prop="image">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img :src="scope.row.image" style="width:300px"/>
            <img :src="scope.row.image" style="max-width: 50px;max-height: 50px" slot="reference">
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="购买数量" align="center" prop="count"/>
      <el-table-column label="审批时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="auditUserName"/>
      <el-table-column label="退款金额" align="center" prop="refundPrice"/>
      <el-table-column label="第三方支付流水号(如支付宝)" align="center" prop="refundTradeNo"/>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template
          slot-scope="scope"
          v-if="scope.row.status === '0'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qiushi:mall:tradeaftersale:edit']"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="售后单号" prop="no">
          <span>{{form.no}}</span>
        </el-form-item>
        <el-form-item label="商品名称" prop="spuName">
          <span>{{form.spuName}}</span>
        </el-form-item>
        <el-form-item label="商品类型" prop="spuType">
          <span>{{form.spuType}}</span>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <span>{{form.refundPrice}}</span>
        </el-form-item>

        <el-form-item label="退款状态" prop="status">
          <el-select v-model="form.bullst" placeholder="谨慎选择退款状态！">
            <el-option
              v-for="item in bullst_dict"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSale, getSale, delSale, addSale, updateSale} from "@/api/qiushi/mall/tradeaftersale";

export default {
  name: "Tradeaftersale",
  data() {
    return {
      // 字典
      bullst_dict: [{
        value: '0',
        label: '同意（不退款）'
      },{
        value: '1',
        label: '同意（退款）'
      },{
        value: '2',
        label: '拒绝'
      },],
      spuType_dict: [{
        value: '1',
        label: '积分商品'
      },{
        value: '2',
        label: '普通商品'
      },{
        value: '3',
        label: '服务商品'
      },],
      status_dict: [{
        value: '0',
        label: '申请中'
      },{
        value: '1',
        label: '等待退款'
      },{
        value: '2',
        label: '同意'
      },{
        value: '3',
        label: '拒绝'
      },],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后订单表格数据
      saleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        no: null,
        status: null,
        applyReason: null,
        applyDescription: null,
        applyPicUrls: null,
        applyTime: null,
        orderId: null,
        orderNo: null,
        orderItemId: null,
        spuId: null,
        spuName: null,
        image: null,
        count: null,
        auditTime: null,
        auditUserId: null,
        auditReason: null,
        refundPrice: null,
        refundTradeNo: null,
        refundTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // userId: [
        //   {required: true, message: "用户ID不能为空", trigger: "blur"}
        // ],
        // no: [
        //   {required: true, message: "售后单号不能为空", trigger: "blur"}
        // ],
        // status: [
        //   {required: true, message: "售后状态  0申请中  1等待退款  2退款成功 3拒绝不能为空", trigger: "change"}
        // ],
        // applyReason: [
        //   {required: true, message: "申请原因不能为空", trigger: "blur"}
        // ],
        // orderId: [
        //   {required: true, message: "订单编号不能为空", trigger: "blur"}
        // ],
        // orderNo: [
        //   {required: true, message: "订单流水号不能为空", trigger: "blur"}
        // ],
        // orderItemId: [
        //   {required: true, message: "订单项编号不能为空", trigger: "blur"}
        // ],
        // spuId: [
        //   {required: true, message: "商品 SPU 编号不能为空", trigger: "blur"}
        // ],
        // spuName: [
        //   {required: true, message: "商品 SPU 名称不能为空", trigger: "blur"}
        // ],
        // count: [
        //   {required: true, message: "购买数量不能为空", trigger: "blur"}
        // ],
        // refundPrice: [
        //   {required: true, message: "退款金额不能为空", trigger: "blur"}
        // ],
        // delFlag: [
        //   {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        // ],
        // createTime: [
        //   {required: true, message: "创建时间不能为空", trigger: "blur"}
        // ],
        // updateTime: [
        //   {required: true, message: "更新时间不能为空", trigger: "blur"}
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询售后订单列表 */
    getList() {
      this.loading = true;
      listSale(this.queryParams).then(response => {
        this.saleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bullst: 0,
        id: null,
        userId: null,
        userName: null,
        no: null,
        status: null,
        applyReason: null,
        applyDescription: null,
        applyPicUrls: null,
        applyTime: null,
        orderId: null,
        orderNo: null,
        orderItemId: null,
        spuId: null,
        spuName: null,
        spuType: null,
        image: null,
        count: null,
        auditTime: null,
        auditUserId: null,
        auditUserName: null,
        auditReason: null,
        refundPrice: null,
        refundTradeNo: null,
        refundTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSale(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改售后订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          /** 图片上传 */
          // if (this.form.image != null && this.form.image.startsWith("/profile/upload"))
          //   this.form.image = process.env.VUE_APP_BASE_API + this.form.image
          // if (this.form.applyPicUrls != null && this.form.applyPicUrls.startsWith("/profile/upload"))
          //   this.form.applyPicUrls = process.env.VUE_APP_BASE_API + this.form.applyPicUrls

          if (this.form.id != null) {
            updateSale(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSale(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除售后订单编号为"' + ids + '"的数据项？').then(function () {
        return delSale(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/mall/tradeaftersale/export', {
        ...this.queryParams
      }, `sale_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
